<?php
/**
 * See the enclosed file license.txt for licensing information.
 * If you did not receive this file, get it at http://www.fsf.org/copyleft/gpl.html
 *
 * @copyright   The XOOPS project http://www.xoops.org/
 * @license     http://www.fsf.org/copyleft/gpl.html GNU General Public License (GPL)
 * @package     installer
 * @since       2.3.0
 * @author      Haruki Setoyama  <haruki@planewave.org>
 * @author      Kazumi Ono <webmaster@myweb.ne.jp>
 * @author      Skalpa Keo <skalpa@xoops.org>
 * @author      Taiwen Jiang <phppp@users.sourceforge.net>
 * @author      DuGris (aka L. JEN) <dugris@frxoops.org>
 * @version     $Id: checklogin.php 1429 2008-04-02 12:27:19Z phppp $
**/

if (!defined("XOOPS_ROOT_PATH"))  { die('XOOPS Custom Installation die'); }

define("XOOPS_THEME_PATH", XOOPS_ROOT_PATH . "/themes");
define("XOOPS_UPLOAD_PATH", XOOPS_ROOT_PATH . "/uploads");
define("XOOPS_THEME_URL", XOOPS_URL . "/themes");
define("XOOPS_UPLOAD_URL", XOOPS_URL . "/uploads");

// ----- BEGIN: Deprecated, move to template class -----
//define("SMARTY_DIR", XOOPS_ROOT_PATH . "/class/smarty/");
define("XOOPS_COMPILE_PATH", XOOPS_VAR_PATH . "/caches/smarty_compile");
define("XOOPS_CACHE_PATH", XOOPS_VAR_PATH . "/caches/xoops_cache");
// ----- END: Deprecated, move to template class -----

include_once XOOPS_ROOT_PATH.'/include/functions.php';

require_once XOOPS_ROOT_PATH.'/class/database/databasefactory.php';
$GLOBALS['xoopsDB'] = $xoopsDB =$xoopsDB =& XoopsDatabaseFactory::getDatabaseConnection();

include_once XOOPS_ROOT_PATH.'/kernel/object.php';
include_once XOOPS_ROOT_PATH.'/class/criteria.php';
include_once XOOPS_ROOT_PATH.'/class/module.textsanitizer.php';

$config_handler =& xoops_gethandler('config');
$xoopsConfig = $config_handler->getConfigsByCat(XOOPS_CONF);


include_once XOOPS_ROOT_PATH.'/language/'.$xoopsConfig['language'].'/global.php';
include_once XOOPS_ROOT_PATH.'/language/'.$xoopsConfig['language'].'/user.php';

$uname = !isset($_REQUEST['uname']) ? '' : trim($_REQUEST['uname']);
$pass = !isset($_REQUEST['pass']) ? '' : trim($_REQUEST['pass']);
if ($uname == '' || $pass == '') {
    redirect_header(XOOPS_URL.'/user.php', 1, _US_INCORRECTLOGIN);
    exit();
}

$myts =& MyTextsanitizer::getInstance();
$member_handler =& xoops_gethandler('member');

include_once XOOPS_ROOT_PATH.'/class/auth/authfactory.php';
include_once XOOPS_ROOT_PATH.'/language/'.$xoopsConfig['language'].'/auth.php';
$xoopsAuth =& XoopsAuthFactory::getAuthConnection($myts->addSlashes($uname));
$user = $xoopsAuth->authenticate($myts->addSlashes($uname), $myts->addSlashes($pass));

if (false != $user) {
	if (0 == $user->getVar('level')) {
		redirect_header(XOOPS_URL.'/index.php', 5, _US_NOACTTPADM);
		exit();
	}

	$xoopsUser = & $member_handler->getUser( $user->getVar('uid') );
	if ( $xoopsUser->isAdmin() ) {
		$config_handler =& xoops_gethandler('config');
		$xoopsConfig = $config_handler->getConfigsByCat(XOOPS_CONF);
		setcookie( 'xo_install_lang', $xoopsConfig['language'], null, null, null );

		$_SESSION['UserLogin'] = true;
		$_SESSION['Language'] = $xoopsConfig['language'];

		$_REQUEST['xoops_redirect'] = trim( $_REQUEST['xoops_redirect'] );
		$parsed = parse_url(XOOPS_URL);
		$url = isset($parsed['scheme']) ? $parsed['scheme'].'://' : 'http://';
		if ( isset( $parsed['host'] ) ) {
			$url .= $parsed['host'];
			if ( isset( $parsed['port'] ) ) {
				$url .= ':' . $parsed['port'];
			}
		} else {
			$url .= $_SERVER['HTTP_HOST'];
		}
		if ( @$parsed['path'] ) {
			if ( strncmp( $parsed['path'], $_POST['xoops_redirect'], strlen( $parsed['path'] ) ) ) {
				$url .= $parsed['path'];
			}
		}
		$url .= $_POST['xoops_redirect'];
		redirect_header($url, 1, sprintf(_US_LOGGINGU, $user->getVar('uname')), false);
		exit();
	}
}

if ( !isset($_SESSION['UserLogin']) || !$_SESSION['UserLogin'] ) {
	redirect_header(XOOPS_URL.'/index.php', 5, _US_NOACTTPADM);
	exit();
}

?>